package com.haierp.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.haierp.model.BuySiteDetail;
import com.haierp.model.Item;
import com.haierp.service.IBuySiteDetailService;
import com.haierp.util.JsonPageResult;
import com.haierp.util.JsonResult;
import com.haierp.vo.BuySiteDetailQueryVO;
import com.wangzhixuan.commons.base.BaseController;
import com.wangzhixuan.commons.shiro.ShiroUser;
import com.wangzhixuan.commons.utils.StringUtils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 采购明细处理器
 * @author wangkan
 *
 */
@Controller
@RequestMapping("/buySiteDetail")
public class BuySiteDetailController extends BaseController {

	@Autowired
	private IBuySiteDetailService iBuySiteDetailService;
	/**
	 * 采购站点明细
	 * @param
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping(value = "/add")
	@ResponseBody
	public Object add(String siteArr) throws IOException {
		JsonResult<BuySiteDetail> result = new JsonResult<>();
		ShiroUser shiroUser = this.getShiroUser();
		if(StringUtils.isNotBlank(siteArr)) {
			JSONArray siteArrTemp = JSONArray.fromObject(siteArr);
			for (Object object : siteArrTemp) {
				JSONObject fromObject = JSONObject.fromObject(object);
				BuySiteDetail buySiteDetail = new BuySiteDetail();
				buySiteDetail.setCompanyId(shiroUser.getCompanyId());
				buySiteDetail.setGmtCreate(new Date());
				buySiteDetail.setGmtModify(new Date());
				buySiteDetail.setBuyCity(fromObject.getString("buyCity"));
				buySiteDetail.setBuySite(fromObject.getString("buySite"));
				buySiteDetail.setStatus(fromObject.getInt("status"));
				JSONArray sitearrone = fromObject.getJSONArray("siteArrOne");
				for (Object object2 : sitearrone) {
					JSONObject fromObject2 = JSONObject.fromObject(object2);
					buySiteDetail.setShopSite(fromObject2.getString("shopSite"));
					buySiteDetail.setBuyTax(fromObject2.getDouble("buyTax"));
					iBuySiteDetailService.addBuySiteDetail(buySiteDetail);
				}
			}
		} else {
			return result.buildMsg("新增"+siteArr+"数据不存在").buildIsSuccess(false);
		}
        return result.buildIsSuccess(true);
	}

	/**
	 * 更新采购明细
	 * @param
	 * @return
	 */
	@RequestMapping("/update")
	@ResponseBody
	public Object update(BuySiteDetail buySiteDetail) {
		JsonResult<Item> result = new JsonResult<>();
		if (buySiteDetail.getId() == null) {
			return result.buildIsSuccess(false).buildMsg("没有 id");
		} else {
			ShiroUser one = this.getShiroUser();
			buySiteDetail.setCompanyId(one.getCompanyId());
			buySiteDetail.setGmtModify(new Date());
			iBuySiteDetailService.updateBuySiteDetail(buySiteDetail);
			return result.buildIsSuccess(true);
		}
	}

	
	@RequestMapping("/query")
	@ResponseBody
	public Object query(Long id) {
		JsonResult<BuySiteDetail> result = new JsonResult<>();
		if (id != null) {
			BuySiteDetail item = iBuySiteDetailService.queryBuySiteDetail(id);		
			result.setData(item);
			return result.buildIsSuccess(true);
		} else {
			return result.buildIsSuccess(false).buildMsg("没有 id");
		}
	}

	
	@RequestMapping("/delete")
	@ResponseBody
	public Object delete(Long id) {
		JsonResult<String> result = new JsonResult<>();
		try {
			BuySiteDetail buySiteDetail = iBuySiteDetailService.selectById(id);
			if(buySiteDetail==null){
				return JsonResult.buildFailed("删除对象不存在！");
			}
			iBuySiteDetailService.deleteSelective(buySiteDetail);
		} catch(Exception e) {
			return result.buildIsSuccess(false).buildMsg(e.getMessage());
		}
		return result.buildIsSuccess(true);
	}
	
	@RequestMapping("/queryBuySiteDetail")
	@ResponseBody
	public Object queryItems(BuySiteDetailQueryVO buySiteDetailQueryVO) {
		JsonPageResult<List<BuySiteDetail>> result = iBuySiteDetailService.queryBuySiteDetail(buySiteDetailQueryVO);
		return result.buildIsSuccess(true);
	}
	
	

}
